package com.meeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meeting.common.result.Result;
import com.meeting.entity.MediaDeviceType;
import com.meeting.entity.MeetingRoomDevice;
import com.meeting.exception.BusinessException;
import com.meeting.mapper.MeetingRoomDeviceMapper;
import com.meeting.model.dto.mediadevicetype.MediaDeviceTypeAddRequest;
import com.meeting.model.dto.mediadevicetype.MediaDeviceTypeModifyRequest;
import com.meeting.model.dto.mediadevicetype.MediaDeviceTypeQueryRequest;
import com.meeting.model.vo.MediaDeviceTypeVO;
import com.meeting.service.MediaDeviceTypeService;
import com.meeting.mapper.MediaDeviceTypeMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 25843
* @description 针对表【media_device_type(多媒体设备类型表)】的数据库操作Service实现
* @createDate 2025-04-19 12:54:48
*/
@Service
public class MediaDeviceTypeServiceImpl extends ServiceImpl<MediaDeviceTypeMapper, MediaDeviceType>
    implements MediaDeviceTypeService{

    @Resource
    private MeetingRoomDeviceMapper meetingRoomDeviceMapper;

    @Override
    public Wrapper<MediaDeviceType> getQueryWrapper(MediaDeviceTypeQueryRequest queryRequest) {
        LambdaQueryWrapper<MediaDeviceType> wrapper = new LambdaQueryWrapper<>();
        if (queryRequest == null) {
            return wrapper;
        }
        Integer id = queryRequest.getId();
        String name = queryRequest.getName();
        String description = queryRequest.getDescription();
        wrapper.eq(id != null, MediaDeviceType::getId, id);
        wrapper.like(StringUtils.isNotBlank(name), MediaDeviceType::getName, name);
        wrapper.like(StringUtils.isNotBlank(description), MediaDeviceType::getDescription, description);
        return wrapper;
    }

    @Override
    public Boolean addMediaDeviceType(MediaDeviceTypeAddRequest queryRequest) {
        String name = queryRequest.getName();
        String description = queryRequest.getDescription();
        // 判断设备类型名字是否重复
        LambdaQueryWrapper<MediaDeviceType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MediaDeviceType::getName, name);
        MediaDeviceType one = this.getOne(wrapper);
        if (one != null) {
            throw new BusinessException(400, "设备类型已存在");
        }
        MediaDeviceType mediaDeviceType = new MediaDeviceType();
        mediaDeviceType.setName(name);
        mediaDeviceType.setDescription(description);
        return this.save(mediaDeviceType);
    }

    @Override
    public Boolean modifyMediaDeviceType(MediaDeviceTypeModifyRequest modifyRequest) {
        Integer id = modifyRequest.getId();
        String name = modifyRequest.getName();
        String description = modifyRequest.getDescription();
        LambdaQueryWrapper<MediaDeviceType> wrapper = new LambdaQueryWrapper<>();
        // 判断设备类型是否存在
        wrapper.eq(MediaDeviceType::getId, id);
        MediaDeviceType one = this.getOne(wrapper);
        if (one == null) {
            throw new BusinessException(400, "设备不存在");
        }
        MediaDeviceType mediaDeviceType = new MediaDeviceType();
        mediaDeviceType.setId(id);
        mediaDeviceType.setName(name);
        mediaDeviceType.setDescription(description);
        return this.updateById(mediaDeviceType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteMediaDeviceType(Long id) {
        LambdaQueryWrapper<MediaDeviceType> wrapper = new LambdaQueryWrapper<>();
        // 判断设备类型是否存在
        wrapper.eq(MediaDeviceType::getId, id);
        MediaDeviceType one = this.getOne(wrapper);
        if (one == null) {
            throw new BusinessException(400, "设备不存在");
        }
        // 删除会议室和设备关系
        LambdaQueryWrapper<MeetingRoomDevice> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(MeetingRoomDevice::getDeviceTypeId, id);
        meetingRoomDeviceMapper.delete(wrapper1);
        return this.removeById(id);
    }

    /**
     * 获取会议室设备类型
     *
     * @param meetingRoomId
     * @return
     */
    @Override
    public Result<List<MediaDeviceTypeVO>> getMediaDeviceTypeListByMeetingRoomId(Integer meetingRoomId) {
        List<MediaDeviceType> mediaDeviceTypes = this.baseMapper.getMediaDeviceTypeListByMeetingRoomId(meetingRoomId);
        List<MediaDeviceTypeVO> list = mediaDeviceTypes.stream().map(mediaDeviceType -> {
            MediaDeviceTypeVO mediaDeviceTypeVO = new MediaDeviceTypeVO();
            mediaDeviceTypeVO.setId(mediaDeviceType.getId());
            mediaDeviceTypeVO.setName(mediaDeviceType.getName());
            mediaDeviceTypeVO.setDescription(mediaDeviceType.getDescription());
            return mediaDeviceTypeVO;
        }).toList();
        return Result.success(list);
    }
}




